#include "service.h"


Service::Service( int id, const  wchar_t* dll_path)
{
	m_id = id;
	m_dll_path = dll_path;
	m_handle = NULL;
	IsInit = false;
	cout <<  "Service" << this->m_id << " is created" << endl;
}

Service::~Service(void)
{
	FreeLibrary(m_handle);
	cout << "Service" <<  this->m_id <<" is deleted" <<  endl;
}

bool Service::Init()
{
	m_handle = LoadLibrary(m_dll_path);
	if (m_handle != NULL)
	{
		if ( (ExecuteService = (pfExecuteService)GetProcAddress(m_handle, "DoSomething")) == NULL)
		{
			cout << "GetProcAddress fail" << endl;
			IsInit = false;
			return false;
		}
	}
	else
	{
		cout << "LoadLibrary fail" << endl;
		IsInit = false;
		return false;
	}
	cout <<  "Service" << this->m_id << " is Initialized" << endl;
	IsInit = true;
	return true;
}

void Service::Execute(Arg *pA)
{
	if (IsInit)
	{
		(*this->ExecuteService) (pA);
	}
	else
	{
		cout << "Service can not be excuted" << endl;
	}
}

void Service::Execute()
{
	//(*this->ExecuteService) ();
}




